package com.bjsxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bisxt.dto.CoinDto;
import com.bisxt.feign.CoinServiceFeign;
import com.bjsxt.dto.MarketDto;
import com.bjsxt.mappers.MarketDtoMappers;
import com.bjsxt.service.ConfigService;
import com.bjsxt.service.TurnoverOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.domain.Market;
import com.bjsxt.mapper.MarketMapper;
import com.bjsxt.service.MarketService;
import org.springframework.util.CollectionUtils;

@Service
@Slf4j
public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> implements MarketService{


    @Autowired
    private CoinServiceFeign coinServiceFeign;





    @Autowired
    private TurnoverOrderService turnoverOrderService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ConfigService configService;



    @Override
    public Page<Market> findByPage(Page<Market> page, Long tradeAreaId, Byte status) {
        return page(page,new LambdaQueryWrapper<Market>().eq(Market::getTradeAreaId,tradeAreaId).eq(Market::getStatus,status));
    }

    @Override
    public List<Market> getMarkersByTradeAreaId(Long id) {


        return list(new LambdaQueryWrapper<Market>().eq(Market::getTradeAreaId,id)
            .eq(Market::getStatus,1)
                .orderByAsc(Market::getSort)
            );
    }

    @Override
    public Market getMarkBySymbol(String symbol) {
        return getOne(new LambdaQueryWrapper<Market>().eq(Market::getSymbol,symbol));
    }




    @Override
    public boolean save(Market entity) {

        log.info("开始新增市场数据");
        Long sellCoinId = entity.getSellCoinId();
        Long buyCoinId = entity.getBuyCoinId();
        List<CoinDto> coins = coinServiceFeign.findCoins(Arrays.asList(sellCoinId, buyCoinId));
        if(CollectionUtils.isEmpty(coins)||coins.size()!=2){
            throw  new IllegalArgumentException("货币输入错误");
        }
        CoinDto coinDto = coins.get(0);
        CoinDto coinDto1 = coins.get(1);
        CoinDto sellCOIN = null;
        CoinDto buyCOIN = null;
        if(coinDto.getId().equals(sellCoinId)){
            sellCOIN = coinDto;
            buyCOIN = coinDto1;
        }else {
            sellCOIN = coinDto1;
            buyCOIN = coinDto;
        }
        entity.setName(sellCOIN.getName()+"/"+buyCOIN.getName()); //交易市场名称
        entity.setTitle(sellCOIN.getTitle()+"/"+buyCOIN.getTitle()); //交易市场标题
        entity.setSymbol(sellCOIN.getImg()+buyCOIN.getName()); //交易市场标识
        entity.setImg(sellCOIN.getImg()); //交易市场图标

        return super.save(entity);
    }



    /**
     * 使用报价货币和基础货币查询市场
     *
     * @param buyCoinId
     * @param sellCoinId
     * @return
     */
    @Override
    public MarketDto findByCoinId(Long buyCoinId, Long sellCoinId) {
        LambdaQueryWrapper<Market> eq = new LambdaQueryWrapper<Market>()
                .eq(Market::getBuyCoinId, buyCoinId)
                .eq(Market::getSellCoinId, sellCoinId)
                .eq(Market::getStatus, 1);
        Market one = getOne(eq);
        if (one == null) {
            return null;
        }
        MarketDto marketDto = MarketDtoMappers.INSTANCE.toConvertDto(one);
        return marketDto;
    }

    /**
     * 查询所有的市场数据
     *
     * @return
     */
    @Override
    public List<MarketDto> queryAllMarkets() {
        List<Market> list = list(new LambdaQueryWrapper<Market>().eq(Market::getStatus, 1));
        return MarketDtoMappers.INSTANCE.toConvertDto(list);
    }

    @Override
    public List<Market> queryByAreaId(Long id) {
        return null;
    }




}
